package com.lee.forum.services.impl;

import com.lee.forum.common.AppResult;
import com.lee.forum.common.ResultCode;
import com.lee.forum.dao.BoardMapper;
import com.lee.forum.exception.ApplicationException;
import com.lee.forum.model.Board;
import com.lee.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-09-30
 * Time: 13:14
 */
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    // 注入dao
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        // 1.参数校验
        if (num == null || num <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 2.调用DAO查询数据库
        List<Board> boards = boardMapper.selectByNum(num);
        // 3.返回结果
        return boards;
    }

    @Override
    public Board selectById(Long id) {
        // 1.参数校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 2.根据板块id查询板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        // 3.返回结果
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        // 1.参数校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 2.根据板块id查询板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        // 判断查询到的板块是否有效
        if (board == null) {
            // 打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString()+"board id = "+id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 3.更新板块下帖子数量
        // 3.1构造数据
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        // 3.2调用DAO执行更新
        int updateRow = boardMapper.updateByPrimaryKeySelective(updateBoard);
        // 4.判断受影响的行数
        if (updateRow <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED+"受影响的行数不等于1.");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        // 5.打印成功日志
        log.info("板块 board id = "+board.getId()+"帖子数量+1");
    }

    @Override
    public void subOneArticleCountById(Long id) {
        // 1.参数校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 2.调用DAO查询板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        // 判断查询到的板块是否有效
        if (board == null) {
            // 打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString()+"board id = "+id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 3.构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        // 判断ArticleCount是否小于0
        if (updateBoard.getArticleCount() < 0) {
            // 如果小于0则设置为0
            updateBoard.setArticleCount(0);
        }
        // 4.调用DAO更新帖子数量
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        // 判断受影响的行数
        if (row <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED+"受影响的行数不等于1.");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

}
